package com.cskaoyan.market.service;

import com.cskaoyan.market.bo.WxCommentBo;
import com.cskaoyan.market.bo.CommentPostBo;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.vo.CommentInfoVo;
import com.cskaoyan.market.vo.CommentVo;
import com.cskaoyan.market.vo.UserInfoVo;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chenjian
 * @Date: 2024/5/20 14:56
 */
@Service
public class WxCommentServiceImpl implements WxCommentService{
    @Autowired
    SecurityManager securityManager;
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;


    @Override
    public CommentVo queryCommentVo(Integer id) {
        CommentVo commentVo=new CommentVo();
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(id);
        List<MarketComment> marketCommentList = marketCommentMapper.selectByExample(commentExample);
        commentVo.setCount(marketCommentList.size());
        commentVo.setData(marketCommentList);
        return commentVo;
    }

    @Override
    public List<CommentInfoVo> list(int valueId, int type, int limit, int page, int showType) {
        UserInfoVo userInfoVo = new UserInfoVo();
        CommentInfoVo commentInfoVo = new CommentInfoVo();
        List<CommentInfoVo> comments = new ArrayList<>();
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        if(showType==1){
            criteria.andHasPictureEqualTo(true);
        }
        criteria.andTypeEqualTo((byte) type);
        PageHelper.startPage(page,limit);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        MarketUserExample marketUserExample = new MarketUserExample();
        for (MarketComment marketComment : marketComments) {
            MarketUserExample.Criteria criteria1 = marketUserExample.createCriteria();
            criteria1.andIdEqualTo(marketComment.getUserId());
            MarketUser marketUser = marketUserMapper.selectOneByExample(marketUserExample);
            userInfoVo.nickName=marketUser.getNickname();
            userInfoVo.avatarUrl=marketUser.getAvatar();
            commentInfoVo.userInfoVo=userInfoVo;
            commentInfoVo.picList=marketComment.getPicUrls();
            commentInfoVo.content=marketComment.getContent();
            commentInfoVo.addTime=marketComment.getAddTime();
            commentInfoVo.adminContent=marketComment.getAdminContent();
            comments.add(commentInfoVo);
        }
        return comments;
    }

    @Override
    public Map<String, Integer> count(int valueId, int type) {
       int allCount;
       int hasPicCount = 0;
        Map<String, Integer> counts = new HashMap<>();
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        allCount=marketComments.size();
        for (MarketComment marketComment : marketComments) {
            if(marketComment.getHasPicture()){
                hasPicCount++;
            }
        }
        counts.put("allCount", allCount);
        counts.put("hasPicCount", hasPicCount);
        return counts;
    }

    @Override
    public MarketComment post(CommentPostBo commentPostBo,int userId) {
        try{
            MarketComment marketComment = new MarketComment();
            marketComment.setContent(commentPostBo.content);
            marketComment.setType((byte) commentPostBo.type);
            marketComment.setValueId(commentPostBo.valueId);
            marketComment.setHasPicture(commentPostBo.hasPicture);
            marketComment.setPicUrls(commentPostBo.pictureUrls);
            marketComment.setStar((short) commentPostBo.star);
            marketComment.setUserId(userId);
            marketComment.setAddTime(LocalDateTime.now());
            marketComment.setId(null);
            marketComment.setUpdateTime(LocalDateTime.now());
            marketCommentMapper.insert(marketComment);
            return marketComment;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void create(WxCommentBo wxCommentBo) {
        // 创建对象
        MarketComment marketComment = new MarketComment();
        marketComment.setContent(wxCommentBo.getContent());
        marketComment.setHasPicture(wxCommentBo.getHasPicture());
        marketComment.setPicUrls(wxCommentBo.getPicUrls());
        marketComment.setStar(wxCommentBo.getStar());
        //直接从session域中获取数据
        SecurityUtils.setSecurityManager(securityManager);
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getSession().getAttribute("user");
        marketComment.setUserId(user.getId());
        marketComment.setAddTime(LocalDateTime.now());
        marketComment.setAddTime(LocalDateTime.now());
        marketComment.setValueId(999);
        marketComment.setType((byte) 0);
        // 添加到数据库中
        int commentId = marketCommentMapper.insert(marketComment);

        // 将comment对象与订单商品进行关联
        MarketOrderGoods marketOrderGoods = new MarketOrderGoods();
        marketOrderGoods.setId(wxCommentBo.getOrderGoodsId());
        marketOrderGoods.setComment(commentId);
        marketOrderGoodsMapper.updateByPrimaryKeySelective(marketOrderGoods);
    }
}
